package com.ledi.guns.modular.ledipay.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.ledi.guns.common.persistence.dao.BillAlipayMapper;
import com.ledi.guns.common.persistence.model.BillAlipay;
import com.ledi.guns.common.persistence.model.SchoolInfo;
import com.ledi.guns.core.util.SystemConstans;
import com.ledi.guns.modular.ledipay.service.EduPayObserver;
import com.ledi.guns.modular.ledipay.service.EduPaySubject;

/**
 * 文件名称： com.ledi.guns.modular.ledipay.service.impl.EduPayCenter.java<br/>
 * 初始作者： 周登文<br/>
 * 创建日期： 2017年11月30日<br/>
 * 功能说明：教育缴费业务处理中心 <br/>
 * ================================================<br/>
 * Copyright (橘子股份-幼儿事业部) 2012-2017.All rights reserved.<br/>
 */
@Service
public class EduPayCenter implements EduPaySubject {

	public static List<EduPayObserver>	observers	= new ArrayList<EduPayObserver>();	// 观察者：支付宝缴费接口实现类、微信缴费接口实现类
	private Long						billId;											// 账单ID
	private SchoolInfo					school;											// 学校信息
	private String						billDetailId;									// 账单明细ID
	@Autowired
	private BillAlipayMapper			billAlipayMapper;

	@Override
	public EduPayCenter addObserver(EduPayObserver o) {

		if (!observers.contains(o)) {
			observers.add(o);
		}
		return this;
	}

	@Override
	public EduPayCenter deleteObserver(EduPayObserver o) {

		if (observers.contains(o)) {
			observers.remove(o);
		}
		return this;
	}

	@Override
	public Map<String, String> addSchool() {

		Map<String, String> result = new HashMap<String, String>();
		for (EduPayObserver observer : observers) {
			String schoolNo = observer.addSchool(school);
			if (observer instanceof EduPayAliObserver) {
				result.put("aliSchoolNo", schoolNo);
			}
		}
		school = null;
		return result;
	}

	@Override
	@Async
	public void sendBill() {

		observers.forEach(observer -> observer.sendBill(billId));
		billId = null;
	}

	@Override
	public void cancelBill() {

		if (billDetailId != null) {
			for (EduPayObserver observer : observers) {
				if (observer instanceof EduPayAliObserver) {
					BillAlipay billAli = new BillAlipay();
					billAli.setBillDetailId(billDetailId);
					billAli.setBillStatus(SystemConstans.BILL_STATUS_SEND_SUCCESS);
					billAli = billAlipayMapper.selectOne(billAli);
					if (billAli != null) {
						((EduPayAliObserver) observer).syncStatus(billAli.getAlipayOrderNo(),
								EduPayAliObserver.SYNC_STATUS_CLOSE, billDetailId);
					}
				}
			}

		}
	}

	@Override
	public String queryStatus() {

		if (billDetailId != null) {
			for (EduPayObserver observer : observers) {
				if (observer instanceof EduPayAliObserver) {
					BillAlipay billAli = new BillAlipay();
					billAli.setBillDetailId(billDetailId);
					billAli.setBillStatus(SystemConstans.BILL_STATUS_SEND_SUCCESS);
					billAli = billAlipayMapper.selectOne(billAli);
					if (billAli != null) {
						return ((EduPayAliObserver) observer).queryStatus(billAli.getSchoolPid(),
								billAli.getAlipayOrderNo());
					}
				}
			}

		}
		return null;
	}

	public EduPayCenter setBillId(Long billId) {

		this.billId = billId;
		return this;
	}

	public EduPayCenter setSchool(SchoolInfo school) {

		this.school = school;
		return this;
	}

	public EduPayCenter setBillDetailId(String billDetailId) {

		this.billDetailId = billDetailId;
		return this;
	}

	public EduPayCenter(@Autowired EduPayAliObserver aliObserver, @Autowired EduPayWexinObserver wexinObserver) {
		this.addObserver(aliObserver);
		this.addObserver(wexinObserver);
	}

}
